package com.hzy.admin.configs;

import com.hzy.admin.filter.FilterHandlerInterceptor;
import com.hzy.admin.services.system.ISysMenuService;
import com.hzy.admin.services.system.ISysUserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * 请求 过滤器 配置
 * </p>
 *
 * @author HZY
 * @since 2020-04-26
 */
@Configuration
public class HzyAdminWebMvcConfigurer implements WebMvcConfigurer {

	private final ISysUserService sysUserService;
	private final AppConfigs appConfigs;
	private final ISysMenuService sysMenuService;

	public HzyAdminWebMvcConfigurer(ISysUserService sysUserService, AppConfigs appConfigs, ISysMenuService sysMenuService) {
		this.sysUserService = sysUserService;
		this.appConfigs = appConfigs;
		this.sysMenuService = sysMenuService;
	}

	/**
	 * 注册拦截器
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new FilterHandlerInterceptor(sysUserService, sysMenuService, appConfigs))
				.addPathPatterns("/**");
	}

	/**
	 * 解决跨域问题
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOriginPatterns("*")
				.allowedMethods("GET", "POST", "DELETE")
				.allowCredentials(true)
				.maxAge(3600)
				.allowedHeaders("*");
	}
}
